package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车接口实现类
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper ;
    @Autowired
    private DishMapper dishMapper ;
    @Autowired
    private SetmealMapper setmealMapper ;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void insert(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入到购物车的商品是否已经存在
        //这里面其实只是根据当前的用户 id 和传递进来的 setmealId 或者 dishId 进行查询 判断结果有没有返回值就行了
        //但是为了方法的复用 我们这里封装成功一个list集合方法 并且对setmealId和dishId的判空放在动态sql中
        ShoppingCart shoppingCart = new ShoppingCart() ;    //构造一个用于查询的购物车对象
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart); //属性拷贝
        shoppingCart.setUserId(BaseContext.getCurrentId()); //这里面是通过线程池获取的 当用户发请求过来的时候 会单独为这个用户建立一个线程 这个 get 方法是把里面存的id 取出来 因为这个线程是每个用户都有自己的线程 所以这里可以获取到当前用户的 所以不会冲突
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        //如果已经存在 则数量加一
        if (shoppingCartList.size() > 0 && shoppingCartList != null) {
            //商品存在 只需要修改一个数量
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber() + 1);   //update shopping_cart set number = #{number} where id = #{id}
            shoppingCartMapper.updateNumberById(cart) ;
        } else {
            //如果不存在 则添加一条数据 需要构造一条完整的对象
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                //传过来的商品是菜品
                Dish dish = dishMapper.getById(dishId); //获取菜品信息
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            } else {
                //传过来的是套餐信息
                Long setmealId = shoppingCart.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);  //第一次插只有一条
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart) ;
        }


    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //这里面没有查询条件 或者说查询条件就是当前用户的 userId
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        return shoppingCartList ;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    /**
     * 删除一条购物车数据
     *
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //获取用户id
        Long userId = BaseContext.getCurrentId();

        //根据用户id和菜品id或者套餐id查询购物车数据
        ShoppingCart shoppingCart = new ShoppingCart() ;
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        //如果数量为1 则直接删除
        if (shoppingCartList.get(0).getNumber() == 1) {
            shoppingCartMapper.delete(shoppingCart) ;
        } else {
            //如果数量大于1 则数量减1
            shoppingCart = shoppingCartList.get(0) ;
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }

        //如果数量大于1 则数量减1
    }
}
